package com.example.j2cachedemo.controller;

import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.CacheObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * ProjectName: j2cache-demo
 * </p>
 * <p>
 * FullClassname: com.example.j2cachedemo.controller.myController
 * </p>
 * <p>
 * Date: 2024/3/17 11:41
 * </p>
 * <p>
 * Description:
 * </p>
 *
 * @author jianli
 * @version 1.0
 */
@RestController
@RequestMapping("/cache")
public class MyController {
	private String key = "myKey";
	private String region = "rx";
	@Autowired
	private CacheChannel cacheChannel;

	@GetMapping("/getInfos")
	public List<String> getInfos() {
		CacheObject cacheObject = cacheChannel.get(region, key);
		if (cacheObject.getValue() == null) {
			// 缓存中没有找到，查询数据库获得
			List<String> data = new ArrayList<String>();
			data.add("info1");
			data.add("info2");
			// 放入缓存
			cacheChannel.set(region, key, data);
			return data;
		}
		return (List<String>) cacheObject.getValue();
	}

	// 清理指定缓存
	@GetMapping("/evict")
	public String evict() {
		cacheChannel.evict(region, key);
		return "evict success";
	}

	// 检测存在那级缓存
	@GetMapping("/check")
	public String check() {
		int check = cacheChannel.check(region, key);
		return "level:" + check;
	}

	// 检测缓存数据是否存在
	@GetMapping("/exists")
	public String exists() {
		boolean exists = cacheChannel.exists(region, key);
		return "exists:" + exists;
	}

	// 清理指定区域的缓存
	@GetMapping("/clear")
	public String clear() {
		cacheChannel.clear(region);
		return "clear success";
	}
}
